//index.js
//获取应用实例
const app = getApp()
const http = require('../../utils/http');
const common = require('../../utils/common')
Page({
    data: {
        motto: 'Hello World',
        icon: {
            parking: '../images/icon/parking.png',
            coupon: '../images/icon/coupon.png',
            home: '../images/icon/home.png',
            myAt: '../images/icon/myAt.png',
            more: '../images/icon/more.png',
            head: '../images/icon/head.png',
            isUser: '../images/icon/isUser.png',
            score: '../images/icon/score.png',
            order1: '../images/icon/order1.png',
            order2: '../images/icon/order2.png',
            order3: '../images/icon/order3.png',
        },
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getPhoneNumber'),
        BackgroundImg: 'https://syl-estate.oss-cn-shenzhen.aliyuncs.com/template/645505222748631502.jpg',
        date: '请选择生日',
        prohibit: true,
        popHead: false,
        popPhone: false
    },
    //页面显示
    onShow: function () {

    },
    onLoad: function () {
        //  this.getBackgroundImage();

        // 判断是否有sessionKey _openid
        if (app._openid) {
            this.isHead(app._openid);
            this.setData({
                openId: app._openid,
                sessionKey: app._sessionKey,
            })
        } else {

            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    http._get('/mini/login?code=' + res.code).then(res => {
                        this.setData({
                            openId: res.result.openid,
                            sessionKey: res.result.sessionKey,
                        })
                        this.isHead(res.result.openid);
                    })
                }
            })

        }

    },
    bindDateChange(e) {
        this.setData({
            date: e.detail.value,
            prohibit: false,
        })
    },
    //获取背景图片,未用到
    getBackgroundImage: function () {
        http._objGet("/mini/getBackFile").then(res => {
            if (res.code == 200) {
                this.setData({
                    BackgroundImg: res.result[0].url,
                });
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 2000
                });
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)
            }

        })
    },

    //判断是否有手机号码
    isPhone: function (openid) {
        http._get('/mini/isPhone?openid=' + openid).then(res => {
            if (res.code == 200) {
                //已经绑定手机
                console.log("已经绑定手机")
                this.setData({
                    popPhone: false,
                })
            } else {
                this.setData({
                    popPhone: true,
                })
            }
        });
    },

    //判断用户登陆信息
    isHead: function (openid) {
        http._get('/mini/isUserinfo?openid=' + openid).then(res => {
            if (res.code == 200) {
                //已经有用户头像昵称
                this.setData({
                    hasUserInfo: true,
                    userInfo: res.result,
                    popHead: false,
                })
                // 判断有没有手机号
                this.isPhone(openid)
            } else {
                //提示用户注册
                this.setData({
                    userInfo: {},
                    hasUserInfo: false,
                    popHead: true,
                })
            }
        })
    },
    //已经未使用
    logOut: function (e) {
        console.log("退出登陆");
        this.setData({
            userInfo: {},
            hasUserInfo: false
        })
    },

    getPhoneNumber: function (e) {
        if (e.detail.iv) {
            console.log("用户按了允许授权按钮");
            const body = {
                sessionKey: this.data.sessionKey,
                encryptedData: e.detail.encryptedData,
                ivStr: e.detail.iv,
                openid: this.data.openId,
            }
            http._objPost("/mini/savePhone", body).then(res => {
                if (res.code == 200) {
                    this.setData({
                        popPhone: false,
                        popHead: false,
                    })
                } else {
                    this.setData({
                        popPhone: true,
                        popHead: false,
                    })
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(function () {
                        wx.hideToast()
                    }, 2000)
                }
            })
        } else {
            console.log("用户按了拒绝按钮");
            wx.showToast({
                title: "为了您更好的体验,请先同意授权",
                icon: 'none',
                duration: 2000
            });
        }
    },

    //获取用户信息
    bindGetUserInfo: function (e) {
        console.log(e);
        if (e.detail.userInfo) {
            app.globalData.userInfo = e.detail.userInfo
            const body = {
                sessionKey: this.data.sessionKey,
                encryptedData: e.detail.encryptedData,
                ivStr: e.detail.iv,
                openid: this.data.openId,
            }
            http._objPost("/mini/saveUserinfo", body).then(res => {
                if (res.code == 200) {
                    this.setData({
                        popHead: false,
                    })
                    this.isHead(this.data.openId);
                } else {
                    this.setData({
                        popHead: true,
                    })
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(function () {
                        wx.hideToast()
                    }, 2000)
                }
            })
        } else {
            wx.showToast({
                title: "为了您更好的体验,请先同意授权",
                icon: 'none',
                duration: 2000
            });
        }
    },

    goTicket() {
        wx.navigateTo({
            url: '../ticket/ticket'
        })
    },
    goParking: function () {
        wx.navigateTo({
            url: '../parking/parking'
        })
    },
    goScore: function () {
        wx.navigateTo({
            url: '../score/score'
        })
    }
})
